import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { TagEntity } from 'src/api/tag/entities/tag.entity';
import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm';
import { ArticleEntity } from './article.entity';

@Entity('article_tag')
export class ArticleTagEntity extends SharedEntity {
  @Column('varchar', { name: 'article_id', nullable: true, length: 36 })
  articleId: string | null;

  @Column('varchar', { name: 'tag_id', nullable: true, length: 36 })
  tagId: string | null;

  // 对应的文章id
  @ManyToOne(() => ArticleEntity, (article) => article.tags, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'article_id', referencedColumnName: 'id' }])
  article: ArticleEntity;

  // 对应的tag，多个tag对应一个文章
  @ManyToOne(() => TagEntity, (tag) => tag.tags, {
    onDelete: 'RESTRICT',
    onUpdate: 'RESTRICT',
  })
  @JoinColumn([{ name: 'tag_id', referencedColumnName: 'id' }])
  tag: TagEntity;
}
